package com.example.sunlight.unicomsensing;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

import com.example.sunlight.unicomsensing.fragment.BackCarFragment;
import com.example.sunlight.unicomsensing.fragment.CarInfoFragment;
import com.example.sunlight.unicomsensing.fragment.CarMainFragment;

public class MainActivity extends Activity implements View.OnTouchListener {
    RelativeLayout parentPanel;
    BackCarFragment backCarFragment;
    CarMainFragment carMainFragment;
    CarInfoFragment carInfoFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        parentPanel = (RelativeLayout) findViewById(R.id.parentPanel);
        parentPanel.setOnTouchListener(this);

        backCarFragment = new BackCarFragment();
        carMainFragment = new CarMainFragment();
        carMainFragment.setActivity(this);
        carInfoFragment = new CarInfoFragment();
        getFragmentManager().beginTransaction().add(R.id.parentPanel, carMainFragment).commit();
    }

    float starX, endX;
    int num;

    public void toFragment(int num, String state) {
        if (state.equals("right")) {
            switch (num) {
                case 0:
                    getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slide_out_right, R.animator.slide_in_left).replace(R.id.parentPanel, carMainFragment).commit();
                    break;
                case 1:
                    getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slide_out_right, R.animator.slide_in_left).replace(R.id.parentPanel, backCarFragment).commit();
                    break;
                case 2:
                    getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slide_out_right, R.animator.slide_in_left).replace(R.id.parentPanel, carInfoFragment).commit();
                    break;
                case 3:
                    break;
            }
        } else {
            switch (num) {
                case 0:
                    getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slide_in_lefts, R.animator.slide_out_rights).replace(R.id.parentPanel, carMainFragment).commit();
                    break;
                case 1:
                    getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slide_in_lefts, R.animator.slide_out_rights).replace(R.id.parentPanel, backCarFragment).commit();
                    break;
                case 2:
                    getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slide_in_lefts, R.animator.slide_out_rights).replace(R.id.parentPanel, carInfoFragment).commit();
                    break;
            }
        }

    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                starX = motionEvent.getX();
                break;

            case MotionEvent.ACTION_UP:
                endX = motionEvent.getX();
                if (endX - starX < 0) {
                    if (Math.abs(endX - starX) > 50) {
                        Log.e("move", "向右");
                        if (num < 2) {
                            num++;
                            toFragment(num, "right");
                        } else {
                            num = 1;
                        }
                    }
                } else {
                    if (Math.abs(endX - starX) > 50) {
                        Log.e("move", "向左");
                        if (num > 0) {
                            num--;
                            toFragment(num, "left");
                        } else {
                            num = 0;
                        }

                    }
                }

                break;
        }
        return true;
    }
}
